package com.berje.vkalarm;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.berje.vkalarm.vksdk.model.Group;

public class Utils {
	private static final String TAG = "Utils";

	public static Bitmap getGroupBitmap(Group g) {
		Log.d(TAG, "g: " + g);
		Bitmap bp = null;
		String url = g.getPhoto();
		if (url != null) {
			Log.d(TAG, "Loading image " + url);
			try {
				URL aURL = new URL(url);
				URLConnection conn = aURL.openConnection();
				conn.connect();
				InputStream is = conn.getInputStream();
				BufferedInputStream bis = new BufferedInputStream(is);
				bp = BitmapFactory.decodeStream(bis);
				g.setPhotoBitmap(bp);
				bis.close();
				is.close();
			} catch (IOException e) {
				Log.e(TAG, "Error loading image " + url);
			}
		}

		if (bp != null) {
			g.setPhotoBitmap(bp);
		}

		return bp;

	}

}
